package worldinruin.gui.imp;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.ListModel;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import worldinruin.GameDataHandler;
import worldinruin.controllers.ControlFactory;
import worldinruin.controllers.ControlFactory.Controllers;
import worldinruin.core.IMap;
import worldinruin.gui.imp.gameview.gameboard.tiles.MapPanel;
import worldinruin.resources.Components;
import worldinruin.resources.Panels;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CreateNewGamePanel extends javax.swing.JPanel  {
	private JLabel chooseMapLabel;
	private JScrollPane mapListScrollPane;
	private JList mapList;
	private JButton createGameButton;
	private JButton backButton;
	private JTextField player1NameTextField;
	private JTextField player2NameTextField;
	private JTextField player3NameTextField;
	private JTextField player4NameTextField;
	private JComboBox numberOfPlayersComboBox;
	private JLabel enterPlayersLabel;
	private JPanel mapPreviewContainer;
	private IMap map;
	private Action listener;
	private MapPanel mapPreview;
	

	/**
	 * Creates a new panel where it's possible to create games.
	 */
	public CreateNewGamePanel() {
		super();
		initGUI();
		initListener();
	}
	
	/**
	 * Initiate listeners to components in panel.
	 */
	private void initListener() {
		listener = ControlFactory.getControl(Controllers.GAME_CONTROLLER);
		
		backButton.addActionListener(listener);
		listener.putValue(Components.backButton, backButton);
		
		createGameButton.addActionListener(listener);
		listener.putValue(Components.startGameButton, createGameButton);
		listener.putValue(Panels.CREATENEWGAMEPANEL.toString(), this);
	}
	
	/**
	 * Initializes the create new game panel.
	 */
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			setPreferredSize(new Dimension(400, 300));
			{
				chooseMapLabel = new JLabel();
				chooseMapLabel.setText(Messages.getString("CreateNewGamePanel.0")); //$NON-NLS-1$
				chooseMapLabel.setFont(new java.awt.Font("Segoe UI",0,20));
			}
			{
				mapListScrollPane = new JScrollPane();
				mapListScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				{
					
					
					
					final ListModel mapListModel = 
						new DefaultComboBoxModel (
								GameDataHandler.getInstance().getFile().getMapNames().toArray());
				
					mapList = new JList();
					mapList.addListSelectionListener(new ListSelectionListener() {
						public void valueChanged(ListSelectionEvent evt) {
							
							
							if (evt.getValueIsAdjusting()) {
								System.out.println(evt.getFirstIndex());
								System.out.println(evt.getLastIndex());
								map = GameDataHandler.getInstance().getFile().getMap((String) mapListModel.getElementAt(evt.getLastIndex()));							
								map = GameDataHandler.getInstance().getFile().getMap((String) mapListModel.getElementAt(evt.getFirstIndex()));
								
								mapPreviewContainer.remove(mapPreview);								
								mapPreview = new MapPanel(map);
								mapPreviewContainer.add(mapPreview);
								revalidate();

							}	
					    }
						
					});
					mapListScrollPane.setViewportView(mapList);
					mapList.setModel(mapListModel);
					mapList.setPreferredSize(new java.awt.Dimension(560, 353));
				}
			}
			{	
				Object[] mapNames = GameDataHandler.getInstance().getFile().getMapNames().toArray(); 
				
				map = GameDataHandler.getInstance().getFile().getMap((String) mapNames[0]);
				mapPreviewContainer =  new JPanel();
				mapPreview = new MapPanel(map);
				
				mapPreviewContainer.setLayout(new GridLayout(1,1));
				mapPreviewContainer.add(mapPreview);				
				mapPreviewContainer.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			}
			{
				enterPlayersLabel = new JLabel();
				enterPlayersLabel.setText(Messages.getString("CreateNewGamePanel.2")); //$NON-NLS-1$
				enterPlayersLabel.setFont(new java.awt.Font("Segoe UI",0,20));
			}
			{
				ComboBoxModel numberOfPlayersComboBoxModel = 
					new DefaultComboBoxModel(
							new String[] { Messages.getString("CreateNewGamePanel.4"), Messages.getString("CreateNewGamePanel.5"), Messages.getString("CreateNewGamePanel.6") }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				numberOfPlayersComboBox = new JComboBox();
				numberOfPlayersComboBox.setModel(numberOfPlayersComboBoxModel);
				numberOfPlayersComboBox.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						setNumberOfPlayers(getNumberOfPlayers());
					}
				});
			}
			{
				player1NameTextField = new JTextField();
				player1NameTextField.setText(Messages.getString("CreateNewGamePanel.7")); //$NON-NLS-1$
				player1NameTextField.setFont(new java.awt.Font("Segoe UI",0,20));
			}
			{
				player2NameTextField = new JTextField();
				player2NameTextField.setText(Messages.getString("CreateNewGamePanel.9")); //$NON-NLS-1$
				player2NameTextField.setFont(new java.awt.Font("Segoe UI",0,20));
			}
			{
				player3NameTextField = new JTextField();
				player3NameTextField.setText(Messages.getString("CreateNewGamePanel.11")); //$NON-NLS-1$
				player3NameTextField.setFont(new java.awt.Font("Segoe UI",0,20));
			}
			{
				player4NameTextField = new JTextField();
				player4NameTextField.setText(Messages.getString("CreateNewGamePanel.13")); //$NON-NLS-1$
				player4NameTextField.setFont(new java.awt.Font("Segoe UI",0,20));
			}
			setNumberOfPlayers(2);
			{
				backButton = new JButton();
				backButton.setText(Messages.getString("CreateNewGamePanel.15")); //$NON-NLS-1$
				backButton.setFont(new java.awt.Font("Segoe UI",0,20));
			}
			{
				createGameButton = new JButton();
				createGameButton.setText(Messages.getString("CreateNewGamePanel.17")); //$NON-NLS-1$
				createGameButton.setFont(new java.awt.Font("Segoe UI",0,20));
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(chooseMapLabel, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(mapListScrollPane, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 404, GroupLayout.PREFERRED_SIZE)
				    .addComponent(mapPreviewContainer, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 404, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(player1NameTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
				    .addComponent(enterPlayersLabel, GroupLayout.Alignment.BASELINE, 0, 27, Short.MAX_VALUE)
				    .addComponent(numberOfPlayersComboBox, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(player2NameTextField, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(player3NameTextField, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(player4NameTextField, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
				.addGap(40)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(backButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(createGameButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(enterPlayersLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(backButton, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
				                .addGap(42))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(chooseMapLabel, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
				                .addGap(9)))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(numberOfPlayersComboBox, GroupLayout.PREFERRED_SIZE, 454, GroupLayout.PREFERRED_SIZE))
				    .addComponent(mapListScrollPane, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 593, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(player1NameTextField, GroupLayout.Alignment.LEADING, 0, 506, Short.MAX_VALUE)
				    .addComponent(mapPreviewContainer, GroupLayout.Alignment.LEADING, 0, 506, Short.MAX_VALUE)
				    .addComponent(player2NameTextField, GroupLayout.Alignment.LEADING, 0, 506, Short.MAX_VALUE)
				    .addComponent(player3NameTextField, GroupLayout.Alignment.LEADING, 0, 506, Short.MAX_VALUE)
				    .addComponent(player4NameTextField, GroupLayout.Alignment.LEADING, 0, 506, Short.MAX_VALUE)
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(0, 347, Short.MAX_VALUE)
				        .addComponent(createGameButton, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE)))
				.addContainerGap());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Collects all player-strings that will play. If 2 players are selected the list
	 * will only contain 2 player-strings.
	 * @return A list of player-strings.
	 */
	public List<String> getPlayerNames() {
		List<String> playerNameList = new LinkedList<String>();
		if(player1NameTextField.isEditable()) {
			playerNameList.add(player1NameTextField.getText());
			if(player2NameTextField.isEditable()) {
				playerNameList.add(player2NameTextField.getText());
				if(player3NameTextField.isEditable()) {
					playerNameList.add(player3NameTextField.getText());
					if(player4NameTextField.isEditable()) {
						playerNameList.add(player4NameTextField.getText());
					}
				}
			}
		}
		return playerNameList;
	}
	
	/**
	 * Gives the map chosen in the list.
	 * @return The chosen map.
	 */
	public IMap getChosenMap() {
		return map;
	}
	
	/**
	 * This method sets the player number to the user's choice.
	 * The player textfields indicates the player count.
	 * @param i Number of players in the new game.
	 */
	public void setNumberOfPlayers(int i)  {
		player3NameTextField.setEditable(false);
		player4NameTextField.setEditable(false);
		if(i >= 3) {
			player3NameTextField.setEditable(true);
			if(i == 4) {
				player4NameTextField.setEditable(true);
			}
		}
	}
	
	/**
	 * Returns an int of total amount of players.
	 * @return Total players.
	 */
	public int getNumberOfPlayers(){
		return numberOfPlayersComboBox.getSelectedIndex() + 2;
	}
}